﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;

namespace Giraffe.Client.ConversationModule.Converters
{
    //TODO: May be use this workaround for sizing GridView columns
    //<ListView> 
    //    <ListView.View> 
    //        <GridView> 
    //            <GridViewColumn Header="column1" x:Name="col1"/>  
    //            <!--Column that shall resize: Width is set to the Actual Width of the helper field defined below--> 
    //            <GridViewColumn Header="column2" 
    //                Width="{Binding ElementName=helperField, Path=ActualWidth}"/>  
    //        </GridView> 
    //    </ListView.View> 
    //    Test Text  
    //</ListView> 
 
    //<!--This is the hidden helper Grid which does the resizing --> 
    //<Grid Visibility="Hidden">  
    //    <Grid.ColumnDefinitions> 
    //        <!--Width is bound to width of the first GridViewColumn --> 
    //        <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}"/>  
    //        <!--Width is set to "Fill"--> 
    //        <ColumnDefinition Width="*"/>  
    //        <!--Correction Width--> 
    //        <ColumnDefinition Width="10"/>  
    //    </Grid.ColumnDefinitions> 
    //    <!--This is the hidden helper Field which is used to bind to, using the "Fill" column of the helper grid--> 
    //    <Grid Grid.Column="1" x:Name="helperField"/>  
    //</Grid> 

    public class ColumnStrechingConverter: IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.
        ///                 </param><param name="targetType">The type of the binding target property.
        ///                 </param><param name="parameter">The converter parameter to use.
        ///                 </param><param name="culture">The culture to use in the converter.
        ///                 </param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ListView listView = (ListView)value;
            GridView gridView = (GridView)listView.View;
            
            double total = 0;
            foreach (GridViewColumn column in gridView.Columns)
            {
                total += column.Width;
            }
            return (listView.ActualWidth - total);
        }

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.
        ///                 </param><param name="targetType">The type to convert to.
        ///                 </param><param name="parameter">The converter parameter to use.
        ///                 </param><param name="culture">The culture to use in the converter.
        ///                 </param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
